`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: 
// Engineer: 
// 
// Create Date:    17:22:51 03/30/2014 
// Design Name: 
// Module Name:    Four 
// Project Name: 
// Target Devices: 
// Tool versions: 
// Description: 
//
// Dependencies: 
//
// Revision: 
// Revision 0.01 - File Created
// Additional Comments: 
//
//////////////////////////////////////////////////////////////////////////////////
module Song ( audio , sys_CLK , button);
 
	output    audio;
	input     sys_CLK, button;
 
	reg  [23:0] counter4Hz, counter6MHz;
	reg  [13:0] count,origin;
	reg  audiof;
	reg  clk_6MHz, clk_4Hz;
	reg  [4:0]  j;
	reg  [7:0]  len;
 
	assign audio= (button&&(origin!='d0))? audiof : 1'b1 ;  //????
 
	always @(posedge sys_CLK) begin        //6MHz??
		if(counter6MHz==4) begin
			counter6MHz=0;
			clk_6MHz=~clk_6MHz;
		end
		else begin
         counter6MHz=counter6MHz+24'b1;
		end
	end
 
	always @(posedge sys_CLK) begin        //4Hz??
		if(counter4Hz==3125000) begin
			counter4Hz=0;
         clk_4Hz=~clk_4Hz;
		end
		else begin
         counter4Hz=counter4Hz+24'b1;
		end
	end
 
	always @(posedge clk_6MHz) begin
		if(count==16383) begin
         count=origin;
         audiof=~audiof;
		end
		else begin
         count=count+14'b1;
		end
	end
 
	always @(posedge clk_4Hz)  begin
		case(j)
			'd0:origin='d0;
			'd1:origin='d4916;  //low
			'd2:origin='d6168;
			'd3:origin='d7281;
			'd4:origin='d7791;
			'd5:origin='d8730;
			'd6:origin='d9565;
			'd7:origin='d10310;
			'd11:origin='d010647;  //middle
			'd12:origin='d011272;
			'd13:origin='d011831;//559
			'd14:origin='d012087;//256
			'd15:origin='d012556;//469
			'd16:origin='d012974;//418
			'd17:origin='d013346;
			'd21:origin='d13516;  //high
			'd22:origin='d13829;
			'd23:origin='d14108;//279
			'd24:origin='d14235;////
			'd25:origin='d14470;
			'd26:origin='d14678;//208
			'd27:origin='d14864;
			default:origin='d0;
		endcase             
	end
 
	always @(posedge clk_4Hz) begin
		if(len==78)
         len=0;
		else
         len=(button)? len+8'b1 : 8'b0;
		case(len)
			0:j=0;
			1:j=0;
			2:j=0;
			3:j=0;
			4:j=6;
			5:j=0;
			6:j=6;
			7:j=0;
			8:j=13;
			9:j=0;
			10:j=14;
			11:j=13;
			12:j=0;
			13:j=6;
			14:j=6;
			15:j=0;
			16:j=13;
			17:j=13;
			18:j=14;
			19:j=13;
			20:j=0;
			21:j=11;
			22:j=11;
			23:j=6;
			24:j=11;
			25:j=6;
			26:j=11;
			27:j=0;
			28:j=0;
			29:j=21;
			30:j=17;
			31:j=15;
			32:j=0;
			33:j=21;
			34:j=17;
			35:j=15;
			36:j=0;
			37:j=15;
			38:j=14;
			39:j=13;
			40:j=14;
			41:j=13;
			42:j=13;
			43:j=0;
			44:j=13;
			45:j=0;
			46:j=15;
			47:j=15;
			48:j=0;
			49:j=7;
			50:j=11;
			51:j=7;
			52:j=5;
			53:j=7;
			54:j=11;
			55:j=7;
			56:j=5;
			57:j=0;
			
			58:j=21;
			59:j=17;
			60:j=15;
			61:j=0;
			62:j=21;
			63:j=17;
			64:j=15;
			65:j=0;
			66:j=15;
			67:j=14;
			68:j=13;
			69:j=14;
			70:j=13;
			71:j=13;
			72:j=0;
			73:j=13;
			74:j=0;
			75:j=15;
			76:j=15;
			77:j=0;
			
			default: j=0;
		endcase                 
	end
	
endmodule
 